<?php
class questionModelAdmin extends coreFrameworkModel 
{
	
	function __construct()
	{
		parent::__construct();
	}
	//保存商家
	function saveBusiness($data,$id){
		if ($id) {
			$sql = SqlToolsClass::EditData('business',$data,'id='.$id);			
		}else{
			$sql = SqlToolsClass::InsertData('business',$data);
		
		}
		return $this->Execute($sql);
	}
	//查询问题列表
	function selectQuestion($where)
	{
		
		$sql=SqlToolsClass::SelectItem("question",$where,'*',null,'id desc');
		//return $this->GetAll($sql);
		return $this->createPage($sql);
	}
	//问题详情
	function selectQuestionDetial($where)
	{
		$sql=SqlToolsClass::SelectItem("question",'id='.$where,'*',null);
		//return $this->GetAll($sql);
		return $this->createPage($sql);
	}
	//根据id查询商家
	function selectBussessionById($id)
	{
		$sql=SqlToolsClass::SelectItem("business","id=$id");
		return $this->GetRow($sql);
	}
	//问题分类
	function selectQeustionCategory()
	{
		$sql=SqlToolsClass::SelectItem("question_category");
		return $this->GetAll($sql);
	}
	//获取地区
	function selectProvinces($id)
	{
		$sql=SqlToolsClass::SelectItem("area","parent_id=$id",'id,name');
		return $this->GetAll($sql);
	}
	//获取城市
	function selectCity()
	{
		$sql=SqlToolsClass::SelectItem("admin_group",'id!=1 and class=1');
		return $this->GetAll($sql);
	}
	//根据地区获取门店
	function getareashop($where)
	{
		$sql=SqlToolsClass::SelectItem("shop",$where,'id,name',null,'id desc');
		//return $this->GetAll($sql);
		return $this->getAll($sql);
	}
	//获取门店
	function getShop($fatherclassid)
	{
		$sql=SqlToolsClass::SelectItem("admin_group","fatherclass=$fatherclassid");
		return $this->GetAll($sql);
	}
	//获取门店
	function getNewshop($where)
	{
		$sql=SqlToolsClass::SelectItem("shop",$where);
		return $this->GetAll($sql);
	}
	//处理问题
	function savequestion($data,$id)
	{
		$sql = SqlToolsClass::EditData('question',$data,"id=$id");
		return $this->Execute($sql);
	}
	//删除商家
	function delBusiness_manageById($data,$id)
	{
//		$sql=SqlToolsClass::DeleteData("business","id=$id");

		$sql = SqlToolsClass::EditData('business',$data,'id='.$id);
		return $this->GetRow($sql);
	}
	//创建待开发票
	function createTicket($data){
		$sql = SqlToolsClass::InsertData('ticket',$data);
		return $this->Execute($sql);
	}
	//获取商户发票记录
	function getAllTicket($id)
	{
		$join = ' left join '.SqlToolsClass::getTableName('business').' b on b.id = t.shop_id';
		$sql=SqlToolsClass::SelectItem("ticket as t","t.shop_id=$id",'t.price,t.status,t.c_time,b.business_name,t.number,t.*',$join,'t.id desc');
		//echo $sql;die;
		//return $this->GetAll($sql);
		return $this->createPage($sql);
	}
	//获取渠道列表
	function getAllChannel()
	{
		$sql=SqlToolsClass::SelectItem("channel",'','*',null,'id desc');
		return $this->createPage($sql);
	}
	//获取渠道列表
	function getAllChannelNoPage()
	{
		$sql=SqlToolsClass::SelectItem("channel",'','*',null,'id desc');
		return $this->getAll($sql);
	}
	//保存渠道方
	function saveChannel($data,$id){
		if ($id) {
			$sql = SqlToolsClass::EditData('channel',$data,'id='.$id);	
			$res = $this->Execute($sql);	
			return $id;
			/*if ($res) {
				return $id;
			}else{
				return false;
			}*/
		}else{
			$sql = SqlToolsClass::InsertData('channel',$data);
			return $this->Execute($sql);
		}
		
	}
	//保存渠道用户
	function saveChannelUser($data,$id)
	{
		if ($id) {
			$sql = SqlToolsClass::EditData('channel_user',$data,'channel_id='.$id);
		}else{
			$sql = SqlToolsClass::InsertData('channel_user',$data);
		
		}
		return $this->Execute($sql);
	}
	//获取渠道用户信息
	function getChannelUserById($id)
	{
		$sql=SqlToolsClass::SelectItem("channel_user","channel_id=$id");
		return $this->GetRow($sql);
	}
	//获取渠道方信息
	function getChannelById($id)
	{
		$sql=SqlToolsClass::SelectItem("channel","id=$id");
		return $this->GetRow($sql);
	}
	//删除渠道
	function delChannelById($id)
	{
		$sql=SqlToolsClass::DeleteData("channel","id=$id");
		return $this->GetRow($sql);
	}
	//获取渠道应用列表
	function getChannelApp($id)
	{
		if ($id) {
			$where = "a.channel_id=$id";
		}
		$join = ' left join '.SqlToolsClass::getTableName('channel').' c on c.id=a.channel_id';
		$sql=SqlToolsClass::SelectItem("channel_app a",$where,'a.*,c.channel_name',$join,'a.id desc');
		return $this->createPage($sql);
	}
	//保存渠道方应用
	function saveAppChannel($data,$id){
		if ($id) {
			$sql = SqlToolsClass::EditData('channel_app',$data,'id='.$id);			
		}else{
			$sql = SqlToolsClass::InsertData('channel_app',$data);
		
		}
		return $this->Execute($sql);
	}
	//获取渠道 应用 信息
	function getChannelAppById($id)
	{
		$sql=SqlToolsClass::SelectItem("channel_app","id=$id");
		return $this->GetRow($sql);
	}
	//删除渠道
	function delChannelAppById($id)
	{
		$sql=SqlToolsClass::DeleteData("channel_app","id=$id");
		return $this->GetRow($sql);
	}
	//查询没有被渠道应用绑定的用户
	function binding_use($app_data)
	{
		/*if($app_data)
		{
			$str =  "or id in($app_data)";
		}
		$sql=SqlToolsClass::SelectItem("business","author_status=1 ".$str,'*',null,'id desc');
		return $this->GetAll($sql);*/
		$sql=SqlToolsClass::SelectItem("business","channel_id=$app_data",'*',null,'id desc');
		return $this->GetAll($sql);

	}
	//应用绑定商户
	function saveBudingBusiness($data,$id){
			$sql = SqlToolsClass::EditData('channel_app',$data,'id='.$id);
			return $this->Execute($sql);
	}


	//标注已经绑定的商户
	function saveBusinessStatus($selected_id,$data)
	{
		$sql = SqlToolsClass::EditData('business',$data,"id in($selected_id)");
		return $this->Execute($sql);
	}
	//获取商户名称
	function getBusinessName($id)
	{
		$sql=SqlToolsClass::SelectItem("business","id in($id)",'business_name',null);
		return $this->GetAll($sql);
	}
	//发票基本信息
	function getTicketInfo($number){
		$join = ' left join '.SqlToolsClass::getTableName('business').' b on b.id=t.shop_id';
		$sql = SqlToolsClass::SelectItem('ticket as t','t.number="'.$number.'"','t.*,b.business_name,b.taxpayerId,b.taxpayerKey',$join);
		return $this->GetRow($sql);
	}
	//发票查询详细信息
	function getTicketQuery($requestCode){
		$sql = SqlToolsClass::SelectItem('ticket_query','requestCode="'.$requestCode.'"');
		return $this->GetRow($sql);
	}
	
	//更新发票状态
	function updateTicket($data,$where){
		$where = implode(' and ',$where);
		$sql = SqlToolsClass::EditData('ticket',$data,$where);
		
		return $this->Execute($sql);
		
	}
	//发票详细
	function getTicketContent($requestCode){
		$sql = SqlToolsClass::SelectItem('ticket_query','requestCode="'.$requestCode.'"');
		return $this->GetRow($sql);
	}

	//发票图片
	
	function getTicketQueryImages($query_id){
		$sql = SqlToolsClass::SelectItem('ticket_query_images','q_id="'.$query_id.'"');
		return $this->GetAll($sql);
	}
	
	//插入发票查询信息
	function saveTicketQuery($data){
		$sql = SqlToolsClass::SelectItem('ticket_query','requestCode="'.$data['requestCode'].'"');
		
		$query_info = $this->GetRow($sql);
		
		
		if (!$query_info) {
			$sql = SqlToolsClass::InsertData('ticket_query',$data);
			return $this->Execute($sql);
		}else{
			$sql = SqlToolsClass::EditData('ticket_query',$data,'id='.$query_info['id']);
			$res = $this->Execute($sql);
			if ($res) {
				return $query_info['id'];
			}else{
				return false;
			}
			
		}
	}
	//保存发票图片
	function saveTicketQueryImages($datas){
		$sql = SqlToolsClass::DeleteData('ticket_query_images','q_id='.$q_id);
		$this->Execute($sql);
		$sql = SqlToolsClass::InsertDatas('ticket_query_images',$datas);
		return $this->Execute($sql);
		
	}


	//获取应用列表
	function getAppList(){
		$join = ' left join '.SqlToolsClass::getTableName('resources').' r on r.id = c.resources_id'.' left join '.SqlToolsClass::getTableName('channelsite').' cs on cs.id=c.channelsite_id';
		$sql=SqlToolsClass::SelectItem("channel as c","","*,c.c_time,c.id,c.channel_name,cs.channel_name as channelsite_name",$join);
		return $this->GetAll($sql);
	}
	//获取资源方列表
	function getResourcesList(){
		
		$sql=SqlToolsClass::SelectItem("resources",'','*',null,'id desc');
		//return $this->GetAll($sql);
		return $this->createPage($sql);
	}

	//保存应用
	function saveApp($data,$id){
		if ($id) {
			$sql = SqlToolsClass::EditData('channel',$data,'id='.$id);			
		}else{
			$sql = SqlToolsClass::InsertData('channel',$data);
		
		}
		return $this->Execute($sql);
	}
	
	function getChannerData($id){
		$sql = SqlToolsClass::SelectItem('channel','id='.$id);
		return $this->GetRow($sql);
	}
	function getChannelLog($where){
		
		
		$sql = SqlToolsClass::SelectItem('channel_log',$where);
		return $this->GetAll($sql);
	}
	//获取日志按照条件分组
	function getChannelLogGroupBy($where,$group){
		
		
		$sql = SqlToolsClass::SelectItem('channel_log',$where,'*,count(*) as num',"","",$group);
		return $this->GetAll($sql);
	}
	

	//保存资源方信息
	function saveResources($data){
		$sql=SqlToolsClass::InsertData("resources", $data);
		return $this->Execute($sql);
	}
	//查询资源方信息
	function getResources($id){
		$sql=SqlToolsClass::SelectItem("resources","id=$id");
		return $this->GetRow($sql);
	}
	//删除资源方信息
	function delResources($id){
		$sql=SqlToolsClass::DeleteData("resources","id=$id");
		return $this->GetRow($sql);
	}
	//编辑资源信息
	function editResources($data,$id)
	{
		$sql=SqlToolsClass::EditData("resources",$data,"id=$id");
		return $this->GetRow($sql);
	}
	//获取渠道方列表
	function getChannelList()
	{
		$sql=SqlToolsClass::SelectItem("channelsite",'','*',null,'id desc');
		//return $this->GetAll($sql);
		return $this->createPage($sql);
	}
	//获取渠道方列表
	function getChannelListTwo()
	{
		$sql=SqlToolsClass::SelectItem("channelsite",'','*',null,'id desc');
		return $this->GetAll($sql);
	}
	
	//查询渠道方信息
	function getChannel($id){
		$sql=SqlToolsClass::SelectItem("channelsite","id=$id");
		return $this->GetRow($sql);
	}
	//编辑渠道方信息
	function editChannel($data,$id)
	{
		$sql=SqlToolsClass::EditData("channelsite",$data,"id=$id");
		return $this->GetRow($sql);
	}
	//删除渠道方信息
	function delChannel($id){
		$sql=SqlToolsClass::DeleteData("channelsite","id=$id");
		return $this->GetRow($sql);
	}
	//增加商品信息
	function insertTicketGoods($datas){
		$sql = SqlToolsClass::InsertDatas('ticket_goods',$datas);
		return $this->Execute($sql);
	}
	//
	function getApiLogList($where){
		$where = implode(' and ',$where);
		$join = ' left join '.SqlToolsClass::getTableName('channel').' c on c.id=a.channel_id';
		$join .= ' left join '.SqlToolsClass::getTableName('channel_app').' ca on ca.app_key=a.appkey';
		$sql = SqlToolsClass::SelectItem('api_log as a',$where,"a.*,ca.name as app_name,c.channel_name",$join,'a.timestamp desc');
		return $this->createPage($sql);
	}
	//获取发票列表
	function getTicketList($where){
		$where = implode(' and ',$where);
		$sql = SqlToolsClass::SelectItem('ticket',$where);
		return $this->GetAll($sql);
	}
	//
	function getApiLog($where){
		$where = implode(' and ',$where);
		$sql = SqlToolsClass::SelectItem('api_log',$where);
		return $this->getAll($sql);
	}
	//保存发票分类和编码
	function saveClassCode($data,$id){
		if ($id) {
			$sql = SqlToolsClass::EditData('classcode',$data,'id='.$id);			
		}else{
			$sql = SqlToolsClass::InsertData('classcode',$data);
		
		}
		return $this->Execute($sql);
	}
}


?>